package com.yiqi.lottery.feature.documentary.view;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.suozhang.framework.component.recyclerview.DividerItemDecoration;
import com.suozhang.framework.framework.BaseActivity;
import com.suozhang.framework.widget.EmptyView;
import com.yiqi.lottery.R;
import com.yiqi.lottery.entity.bo.LotteryBo;
import com.yiqi.lottery.entity.enums.BangDanType;
import com.yiqi.lottery.feature.documentary.contract.RankContract;
import com.yiqi.lottery.feature.documentary.dependencies.rank.DaggerRankComponent;
import com.yiqi.lottery.feature.documentary.dependencies.rank.RankPresenterModule;
import com.yiqi.lottery.feature.documentary.view.adapter.LotteryListAdapter;

import java.util.List;

import butterknife.BindView;

import javax.inject.Inject;

public class LotteryListActivity extends BaseActivity implements BaseQuickAdapter.OnItemClickListener,RankContract.View, OnRefreshListener, OnLoadMoreListener, BaseQuickAdapter.RequestLoadMoreListener {


    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.rv_lottery_list)
    RecyclerView mRecyclerview;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout mRefreshLayout;
    private LotteryListAdapter mAdapter;
    @Inject
    RankContract.Presenter mPresenter;
    private EmptyView mEmptyView;
    Integer pos;
    private List<LotteryBo> mData;
    private int pageNow=0;
    private int pageSize = 10;
    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_lottery_list;
    }

    @Override
    protected void initInjector() {
        DaggerRankComponent.builder().rankPresenterModule(new RankPresenterModule(this)).build().inject(this);
    }

    @Override
    protected void initView() {
        super.initView();
         pos = this.getIntent().getIntExtra("type", 1);
        initToolBar(toolbar, BangDanType.getName(pos), true, true);

            initAdapter(pos);



    }

    @Override
    protected void initData() {
        pageNow = 0;
        mPresenter.getHallList(pos,pageNow);
    }

    private void initAdapter(int type) {
        mAdapter = new LotteryListAdapter(type);
        mEmptyView = new EmptyView(mRecyclerview);
        mRecyclerview.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerview.addItemDecoration(new DividerItemDecoration(DividerItemDecoration.VERTICAL).setHeight(25));
        mAdapter.bindToRecyclerView(mRecyclerview);
        mAdapter.setOnItemClickListener(this);
        mRefreshLayout.setEnableLoadMore(false);
        mAdapter.setOnLoadMoreListener(this);
        mRefreshLayout.setOnRefreshListener(this);
    }


    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        Intent intent=new Intent(this,PersonalInfoActivity.class) ;
        intent.putExtra("id",mAdapter.getData().get(position).getUser_info().getUser_id());
        startActivity(intent);
    }

    @Override
    public void showRankListSuccess(List<LotteryBo> hallBoPageData) {
        mRefreshLayout.finishRefresh();
        mData = hallBoPageData;
        setData(true, hallBoPageData);
        mAdapter.setEnableLoadMore(true);

    }
    @Override
    public void showRankListMoreSuccess(List<LotteryBo> hallBoPageData) {
        setData(false, hallBoPageData);
    }
    @Override
    public void showRankListError(String error) {
        mRefreshLayout.finishRefresh();
        mAdapter.setEmptyView(mEmptyView.getErrorView(error));
        mAdapter.setNewData(null);
        mAdapter.setEnableLoadMore(true);
    }

    @Override
    public void showRankListEmpty() {
        mRefreshLayout.finishRefresh();
        mAdapter.setEmptyView(mEmptyView.getEmptyView());
        mAdapter.setNewData(null);
    }



    private void setData(boolean isRefresh, List data) {
        pageNow++;
        final int size = data == null ? 0 : data.size();
        if (isRefresh) {
            mAdapter.setNewData(data);
        } else {
            if (size > 0) {
                mAdapter.addData(data);
            }
        }
        if (size < pageSize) {
            //第一页如果不够一页就不显示没有更多数据布局
            mAdapter.loadMoreEnd(isRefresh);
        } else {
            mAdapter.loadMoreComplete();
        }
    }

    @Override
    public void showRankListMoreError(String ex) {
        mAdapter.loadMoreFail();
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        refresh();
    }
    private void refresh() {
        mAdapter.setEnableLoadMore(false);//这里的作用是防止下拉刷新的时候还可以上拉加载
        pageNow = 0;
        mPresenter.getHallList(pos,pageNow);
    }
    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {

    }

    private void loadMore() {
        mPresenter.getHallListMore(pos,pageNow);
    }

    @Override
    public void onLoadMoreRequested() {
        loadMore();
    }
}
